package com.itheima.个人代码.IO流2;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;

public class Demo14 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //objectOutputStream();
        //objectInputStream();
        //ArrayListObjectOutputStream();
        ArrayListObjectInputStream();

    }

    private static void ArrayListObjectInputStream() throws IOException, ClassNotFoundException {
        ObjectInputStream ois=new ObjectInputStream(new FileInputStream
                ("IO流2和对象操作流properties\\stu11.txt"));
        ArrayList<Teacher> list =(ArrayList<Teacher>)ois.readObject();
        ois.close();
        for (Teacher teacher : list) {
            System.out.println(teacher);
        }

    }

    private static void ArrayListObjectOutputStream() throws IOException {
        ArrayList<Teacher> list = new ArrayList<>();
        Collections.addAll(list, new Teacher("zhangsan", 22), new Teacher("lisi", 23)
                , new Teacher("wangwu", 25), new Teacher("maliu", 26));
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream
                ("IO流2和对象操作流properties\\stu11.txt"));
        oos.writeObject(list);
        oos.close();
    }

    private static void objectInputStream() throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream
                ("IO流2和对象操作流properties\\stu5.txt"));
        Teacher teacher = (Teacher) ois.readObject();
        System.out.println(teacher);

    }


    public static void objectOutputStream() throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream
                ("IO流2和对象操作流properties\\stu5.txt"));
        oos.writeObject(new Teacher("chenhui", 18));
        oos.close();
    }
}


class Teacher implements Serializable {
    private String name;
    private int age;
    //private transient int age;
    private static final long serialVersionUID = 42L;

    @Override
    public String toString() {
        return "Teacher{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Teacher() {
    }

    public Teacher(String name, int age) {
        this.name = name;
        this.age = age;
    }
}